home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1959 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Overloading conversion
  5. Date: Sun, 14 Jan 1996 17:05:03 GMT
  6. Organization: Netcom
  7. Message-ID: <30f93747.165768512@nntp.ix.netcom.com>
  8. References: <4d934v$10hs@ds2.acs.ucalgary.ca> <ENNO.96Jan14111826@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. NNTP-Posting-Host: ix-dc18-06.ix.netcom.com
  10. X-NETCOM-Date: Sun Jan 14  9:04:23 AM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner) wrote:
  14.  
  15. |>In article <4d934v$10hs@ds2.acs.ucalgary.ca>
  16. hdang@acs3.acs.ucalgary.ca (Hanna Dang) writes:
  17. |>
  18. |>   Is there a way to overload functions such that certain
  19. |>   functions are called depending on the context the object are
  20. |>   used. For example,
  21. |>
  22. |>   class T {
  23. |>     public:
  24. |>       int& int_value() { modified = TRUE; return value;}
  25. |>       int int_value() {return value;}
  26. |>    private:
  27. |>       int value;
  28. |>       int modified;
  29. |>   }
  30. |>
  31. |>
  32. |>   ...
  33. |>
  34. |>   T a;
  35. |>   int test = a + 6 // int int_value should be called;
  36. |>   a = 20 // int& int_value should be called.
  37. |>
  38. |>   However, this does not work because in both case int& int_value
  39. |>   is called.  Is there another way to find out if value is changed
  40. |>   or it's just inspected.
  41. |>
  42. |>Yes -- make the latter 'int_value' a const member-function, ie.
  43. |>
  44. |>        int int_value() const {return value;}
  45.  
  46. Have you tried this?  If so, what compiler?  I want to make sure I
  47. avoid it.  
  48.  
  49. The decision as to whether to call the const or non-const version of a
  50. member function is based on whether the class object is const, not on
  51. which side of the equal sign it appears.  In both cases above, the
  52. non-const version will be called since a is not const.
  53.  
  54.  
  55. Michael M Rubenstein
  56.